package beautiful.butterfly.drds.data_exchange.report;

import beautiful.butterfly.drds.data_exchange.constant.State;
import org.apache.commons.lang3.Validate;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public final class TaskGroupIdToMessageMap
{
    private static Map<Integer, Message> taskGroupIdToMessageMap =
            new ConcurrentHashMap<Integer, Message>();

    public static void put(
            Integer taskGroupId, Message message)
    {
        taskGroupIdToMessageMap.put(taskGroupId, message);
    }


    public static void updateMessage(final Integer taskGroupId,
                                     final Message message)
    {
        Validate.isTrue(taskGroupIdToMessageMap.containsKey(
                taskGroupId), String.format("taskGroupCommunicationMap中没有注册taskGroupId[%d]的Communication，" +
                "无法更新该taskGroup的信息", taskGroupId));
        taskGroupIdToMessageMap.put(taskGroupId, message);
    }

    public static Message getMessage(int taskGroupId)
    {
        Validate.isTrue(taskGroupId >= 0, "taskGroupId不能小于0");

        return taskGroupIdToMessageMap.get(taskGroupId);
    }


    public static Map<Integer, Message> getTaskGroupIdToMessageMap()
    {
        return taskGroupIdToMessageMap;
    }

    public static Message getMessage()
    {
        Message message = new Message();
        message.setState(State.succeeded);

        for (Message message1 :
                taskGroupIdToMessageMap.values())
        {
            message.merge(message1);
        }

        return message;
    }
}